package oocl.ita.eordering.senJms;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

import oocl.ita.eordering.po.Merchant;

@Component
public class ClientSendJms {
	@Autowired
	@Qualifier(value = "jmsTemplate")
	JmsTemplate template;
	@Autowired
	@Qualifier(value = "queueDestinationComplaint")
	Destination destComplaint;
	@Autowired
	@Qualifier(value = "queueDestinationScore")
	Destination destScore;

	public int CSendJms(Integer oId, String merchantBrand) throws JMSException {
		// TODO Auto-generated constructor stub
		complaintC2A(oId, merchantBrand);
		return 1;
	}

	public int CSendJms(Merchant merchant) throws JMSException {
		scoreC2A(merchant);
		return 1;
	}

	public void complaintC2A(Integer oId, String merchantBrand) throws JMSException {
		template.send(destComplaint, new MessageCreator() {
			String complaintMsg = oId.toString() + "#" + merchantBrand;

			public Message createMessage(Session session) throws JMSException {
				TextMessage msg = session.createTextMessage(complaintMsg);
				return msg;
			}
		});

	}

	public void scoreC2A(Merchant merchant) throws JMSException {
		template.send(destScore, new MessageCreator() {
			String scoreMsg = merchant.getmId() + "#" + merchant.getmStark();

			public Message createMessage(Session session) throws JMSException {
				TextMessage msg = session.createTextMessage(scoreMsg);
				return msg;
			}
		});
	}
}
